package com.example.zselasticsearchsf.config;


import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author: Steven
 * @Date: 2021/8/23 9:29
 */
@Configuration(proxyBeanMethods = false)
public class ESConfig {
    @Value("${es-cluster.nodes-5.hostname}")
    private String hostname_1;

    @Value("${es-cluster.nodes-5.port}")
    private int port_1;

    @Bean
    public RestHighLevelClient restHighLevelClient() {

        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(hostname_1, port_1, "http")
//      ,  new HttpHost(hostname_3, port_3, "http")//多集群
//      ,  new HttpHost(hostname_3, port_3, "http")//多集群
                ));
    }

    @Bean
    ActionListener<AcknowledgedResponse> actionListener(){
        return new ActionListener<AcknowledgedResponse>() {
            @Override
            public void onResponse(AcknowledgedResponse acknowledgedResponse) {
                System.out.println("异步删除索引！！！！！！！");
                System.out.println("isAcknowledged:" + acknowledgedResponse.isAcknowledged());
                System.out.println("acknowledgedResponse:" + acknowledgedResponse.isFragment());
            }

            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
                System.out.println("删除失败！！！！！");
            }
        };
    }

}